angular.module("app.sys").register.controller("ListSysMenuCtrl", ["$scope", "$http", "$state", "$modal", "$modalService", function($scope, $http, $state, $modal, $modalService){
  $scope.stores = [];
  $scope.gets = function(){
    $scope.loadTip = '正在获取数据...........';
    $scope.stores = [];
    $http.post('/sys/menu/gets/', {}, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $scope.stores = ret.data;
        $scope.stores.length == 0 ? $scope.loadTip = '没有找到相关记录！！' : '';
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
  $scope.refresh = function(){
    $scope.stores = [];
    $scope.gets();
  };
  //@formatter:off
  $scope.data = {
    entity:{up_id:0,status:'',is_nav:''},
    opts:{
      statusOpts:[{k:'',t:'请选择'},{k:1,t:'正常'},{k:0,t:'关闭'}],
      menuOpts:[]
    }
  };
  //@formatter:on
  $scope.add = function(){
    if($scope.data.opts.menuOpts.length == 0){
      $http.post('/sys/menu/getUpMenu/', {}, {
        transformRequest: function(data){
          return $.param(data);
        }
      }).success(function(ret){
        if(ret.status){
          $scope.data.opts.menuOpts.push({
            id: 0,
            name: '请选择'
          });
          $.each(ret.data, function(k, v){
            $scope.data.opts.menuOpts.push({
              id: v.id,
              name: v.name
            });
          });
        }else{
          $modalService.myAlert(ret.data);
        }
      }).error(function(){
        $modalService.myAlert('系统错误，请求资源失败！');
      });
    }
    var $modalInstance = $modal.open({
      templateUrl: "/sys/menu/add/",
      controller: "AddSysMenuCtrl",
      backdrop: true,
      resolve: {
        data: function(){
          return $scope.data;
        }
      }
    });
    $modalInstance.result.then(function(status){
      if(status == 'success'){
        $scope.refresh();
      }
    }, function(status){
      if(status == 'success'){
        $scope.refresh();
      }
    });
  };
  $scope.edit = function(id){
    if($scope.data.opts.menuOpts.length == 0){
      $http.post('/sys/menu/getUpMenu/', {}, {
        transformRequest: function(data){
          return $.param(data);
        }
      }).success(function(ret){
        if(ret.status){
          $scope.data.opts.menuOpts.push({
            id: 0,
            name: '请选择'
          });
          $.each(ret.data, function(k, v){
            $scope.data.opts.menuOpts.push({
              id: v.id,
              name: v.name
            });
          });
        }else{
          $modalService.myAlert(ret.data);
        }
      }).error(function(){
        $modalService.myAlert('系统错误，请求资源失败！');
      });
    }
    $http.post('/sys/menu/get/', {
      id: id
    }, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $scope.entity = ret.data;
        var $modalInstance = $modal.open({
          templateUrl: "/sys/menu/edit/",
          controller: "EditSysMenuCtrl",
          backdrop: true,
          resolve: {
            data: function(){
              return {
                entity: $scope.entity,
                opts: $scope.data.opts
              };
            }
          }
        });
        $modalInstance.result.then(function(status){
          if(status == 'success'){
            $scope.refresh();
          }
        }, function(status){
          if(status == 'success'){
            $scope.refresh();
          }
        });
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
  $scope.gets();
}]);
angular.module("app.sys").register.controller("AddSysMenuCtrl", ["$scope", "$http", "$modalInstance", "$modalService", "data", function($scope, $http, $modalInstance, $modalService, data){
  $scope.entity = data.entity;
  $scope.opts = data.opts;
  $scope.cancel = function(){
    $modalInstance.dismiss("cancel");
  };
  $scope.submit = function(){
    $http.post('/sys/menu/add/', $scope.entity, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $modalService.myAlert(ret.data);
        $modalInstance.close('success');
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
}]);
angular.module("app.sys").register.controller("EditSysMenuCtrl", ["$scope", "$http", "$modalInstance", "$modalService", "data", function($scope, $http, $modalInstance, $modalService, data){
  $scope.entity = data.entity;
  $scope.opts = data.opts;
  $scope.cancel = function(){
    $modalInstance.dismiss("cancel");
  };
  $scope.submit = function(){
    $http.post('/sys/menu/edit/', $scope.entity, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $modalService.myAlert(ret.data);
        $modalInstance.close('success');
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
}]);
